<?php
/**
 * Klasse für Projekte
 */
class Model_Billing extends Zend_Db_Table_Abstract
{
    /** Table name */
    protected $_name = 'billing';
    /** Primary Key */
    protected $_primary = 'bid';   
    
	public function createBilling($data) {
    	
    	//Datum in Timestamp umwandeln
    	$data['paydate'] = strtotime($data['paydate']);
		
		//Default Werte aus dem Formular killen
    	switch($data['repetition']) {
    		case 'manually':
    			$data['paydate'] = "";
    			$data['payday'] = "";
    			$data['payweekday'] = "";
    			break;
    		case 'single':
    			$data['payday'] = "";
    			$data['payweekday'] = "";
    			break;
    		case 'weekly':
    			$data['paydate'] = "";
    			$data['payday'] = "";
    			break;
    		case 'monthly':
    			$data['paydate'] = "";
    			$data['payweekday'] = "";
    			break;
    	}
    	
    	//in die Datenbank
    	$lastinsert_id = parent::insert($data);
    	return $lastinsert_id;
    	
    }
    
	public function updateBillingByProjectID($pid, $data) {
    	
    	//Datum in Timestamp umwandeln
    	$data['paydate'] = strtotime($data['paydate']);
    	
    	//Default Werte aus dem Formular killen
    	switch($data['repetition']) {
    		case 'manually':
    			$data['paydate'] = "";
    			$data['payday'] = "";
    			$data['payweekday'] = "";
    			break;
    		case 'single':
    			$data['payday'] = "";
    			$data['payweekday'] = "";
    			break;
    		case 'weekly':
    			$data['paydate'] = "";
    			$data['payday'] = "";
    			break;
    		case 'monthly':
    			$data['paydate'] = "";
    			$data['payweekday'] = "";
    			break;
    	}
		//ProjektID darf nicht überschrieben werden
    	unset($data['pid']);
    	//DB Update
    	$rows_updated = $this->update($data, $this->getAdapter()->quoteInto('pid = ?',$pid));
    	return $rows_updated;
		  	
    }
    
    public function getBillinginformationByProjectID($pid) {
    	
    	//Where-Abfrage muss hier leicht modifiziert werden, damit die Projektnamen auch im Array enthalten sind
		$where = $this->select()
                         	->where('pid = ?', $pid);
    	return $this->fetchRow($where, $order, $offset)->toArray();
    	
    }
    
	public function getProjectIDByBillingID($bid) {
    	
		$billing = $this->find($bid)->toArray();
		return count($billing) == 1 ? $billing[0]['pid'] : false;
    	
    }
    
};

?>